package com.platform.modules.sys.controller;

import com.platform.common.utils.ExcelExport;
import com.platform.modules.sys.dao.SysJmCourseDao;
import com.platform.modules.sys.dao.SysJmCourseUserDao;
import com.platform.modules.sys.dao.SysOrgDao;
import com.platform.modules.sys.entity.SysJmCourseEntity;
import com.platform.modules.sys.entity.SysJmCourseUserEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 导出选课学生信息
 */
@RequestMapping("/course")
@Controller
public class ExportCourseStusController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysJmCourseDao sysCourseDao;
    @Autowired
    private SysJmCourseUserDao sysJmCourseUserDao;
    @Autowired
    private SysOrgDao sysOrgDao;

    /**
     * 导出对应任务的学生信息
     * @param response
     */
    @RequestMapping("/exportStu")
    public void exportInventoryResult(HttpServletResponse response, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        String courseId = request.getParameter("courseId");
        params.put("courseId",courseId);
        List<SysJmCourseUserEntity> list = sysJmCourseUserDao.queryAll(params);
        for(SysJmCourseUserEntity u : list){
            handleUser(u);
        }
        try {
            ExcelExport excelExport = new ExcelExport();
            excelExport.writeBigExcel(response, "学生选课数据", new String[]{"userName", "realName", "orgNo", "position","courseName"}
                    , new String[]{"学号", "姓名", "专业", "班级","任务名称"}
                    , new int[]{30, 30, 30, 30,30}, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void handleUser(SysJmCourseUserEntity u) {
        SysUserEntity userEntity = sysUserService.getById(u.getUserId());
        u.setUserName(userEntity.getUserName());
        u.setRealName(userEntity.getRealName());
        u.setPosition(userEntity.getPosition());
        String orgNoName = sysOrgDao.selectById(userEntity.getOrgNo()).getOrgName();
        u.setOrgNo(orgNoName);
        u.setPosition("人工智能1班"); // TODO 写死再说

    }

    /**导出必选课的学生信息
     *
     * @param response
     */
    @RequestMapping("/exportAllStu")
    public void exportInventoryResult2(HttpServletResponse response, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        params.put("tempFlag","1");
        List<SysUserEntity> users = sysUserService.queryAll(params);
        String courseId = request.getParameter("courseId");
        SysJmCourseEntity course = sysCourseDao.selectById(courseId);
        for(SysUserEntity u : users){
            u.setCourseName(course.getCourseName());
            String orgNoName = sysOrgDao.selectById(u.getOrgNo()).getOrgName();
            u.setOrgNo(orgNoName);
            u.setPosition("人工智能1班");
        }
        try {
            ExcelExport excelExport = new ExcelExport();
            excelExport.writeBigExcel(response, "学生选课数据", new String[]{"userName", "realName", "orgNo", "position","courseName"}
                    , new String[]{"学号", "姓名", "专业", "班级","任务名称"}
                    , new int[]{30, 30, 30, 30,30}, users);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
